<?php
namespace User\Controller;

use Common\Controller\MemberbaseController;
use Common\Log\TypeLog;
use Portal\Model\PostsModel;

class CenterController extends MemberbaseController {

	function _initialize(){
		parent::_initialize();
	}
	
    // 会员中心首页
	public function index() {
		$this->assign($this->user);

        $where = ['l.user_id' => $this->userid, 'l.type' => TypeLog::LOG_TYPE_ARTICLE_BROWSING, 'b.post_type' => PostsModel::POST_TYPE_ARTICLE, 'b.post_status' => PostsModel::POST_STATUS_NORMAL];
        $browsing_history_lists = $this->_lists($where);
        if ($browsing_history_lists['status']) {
            unset($browsing_history_lists['status']);
            $this->assign('browsing_history', $browsing_history_lists);
        }

        $where = ['l.user_id' => $this->userid, 'l.type' => TypeLog::LOG_TYPE_ARTICLE_COLLECT, 'b.post_type' => PostsModel::POST_TYPE_ARTICLE, 'b.post_status' => PostsModel::POST_STATUS_NORMAL];
        $collect_history_lists = $this->_lists($where);
        if ($collect_history_lists['status']) {
            unset($collect_history_lists['status']);
            $this->assign('collect_history', $collect_history_lists);
        }

        $where = ['l.user_id' => $this->userid, 'l.type' => TypeLog::LOG_TYPE_ARTICLE_LIKE, 'b.post_type' => PostsModel::POST_TYPE_ARTICLE, 'b.post_status' => PostsModel::POST_STATUS_NORMAL];
        $like_history_lists = $this->_lists($where);
        if ($like_history_lists['status']) {
            unset($like_history_lists['status']);
            $this->assign('like_history', $like_history_lists);
        }

    	$this->display(':center');
    }

    // 我的足迹
    public function browsing_history()
    {
        $where = ['user_id' => $this->userid, 'type' => TypeLog::LOG_TYPE_ARTICLE_BROWSING];
        $lists = $this->_lists($where);


        if (IS_AJAX) {

            $this->ajaxReturn($lists);

        } else {
            if ($lists['status']) {
                $this->assign("list", $lists['lists']);
                $this->assign('page', $lists['page']);
            }
            $this->display();
        }
    }


    // 我的收藏
    public function collect_history()
    {
        $where = ['user_id' => $this->userid, 'type' => TypeLog::LOG_TYPE_ARTICLE_COLLECT];
        $lists = $this->_lists($where);

        if (IS_AJAX) {

            $this->ajaxReturn($lists);

        } else {
            if ($lists['status']) {
                $this->assign("list", $lists['lists']);
                $this->assign('page', $lists['page']);
            }
            $this->display();
        }
    }

    // 我的点赞
    public function like_history()
    {
        $where = ['user_id' => $this->userid, 'type' => TypeLog::LOG_TYPE_ARTICLE_LIKE];
        $lists = $this->_lists($where);

        if (IS_AJAX) {

            $this->ajaxReturn($lists);

        } else {
            if ($lists['status']) {
                $this->assign("list", $lists['lists']);
                $this->assign('page', $lists['page']);
            }
            $this->display();
        }
    }

    public function _lists($where)
    {
        $model = M('type_log');
        $count = $model
            ->alias("l")
            ->join("__POSTS__ b ON l.item_id = b.id")
            ->where($where)
            ->count();
        $page  = $this->page($count, 10);

        $request_page = I('p');
        if ($request_page > ceil($count / 10)) {
            return array('status' => false);
        }

        $lists = $model
            ->alias("l")
            ->join("__POSTS__ b ON l.item_id = b.id")
            ->join("__USERS__ c ON b.post_author = c.id", 'left')
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order("l.time DESC")
            ->field('l.time, b.post_title, b.id')
            ->select();

        $status = false;
        if ($lists) {
            $status = true;
            foreach ($lists as $k => $v) {
                $v['time'] = time_format(date('Y-m-d H:i:s', $v['time']));
                $lists[$k] = $v;
            }
        }

        $result['status']   = $status;
        $result['lists']    = $lists;
        $result['page']     = $page->show('default');
        $result['count']    = $count;

        return $result;
    }
}
